package com.fosun.sprcore.tools.generatecode.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public abstract class BaseTemplate {
	public abstract void generate(GenerateCodeMain main) throws Exception;

	protected String projectWar;

	public String getProjectWar() {
		return projectWar;
	}

	public void setProjectWar(String projectWar) {
		this.projectWar = projectWar;
	}

	public Map<String, Object> getData(Object model) throws Exception {
		Map<String, Object> ret = new HashMap<String, Object>();
		Field[] field = model.getClass().getDeclaredFields();
		for (int j = 0; j < field.length; j++) { // 遍历所有属性
			String name = field[j].getName();
			String nameU = name.substring(0, 1).toUpperCase() + name.substring(1);
			try {
				Method m = model.getClass().getMethod("get" + nameU);
				Object value = (Object) m.invoke(model);
				
				ret.put(name, value);
				if (value instanceof String) {
					String new_name = (String) value;
					ret.put(name+"U", StringUtils.capitalize(new_name));	
					
				}
							
			} catch (Exception e) {
				//e.printStackTrace();
			}
		}

		return ret;
	}

}
